package com.grts.mall.service;

import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.grts.mall.mapper.ImageMapper;
import com.grts.mall.mapper.ProductMapper;
import com.grts.mall.pojo.Image;
import com.grts.mall.pojo.Product;
import com.grts.mall.pojo.ProductExample;
import com.grts.mall.pojo.ProductExample.Criteria;
import com.grts.mall.pojo.vo.ProductAddVo;
import com.grts.mall.pojo.vo.ProductSearchConditionVo;

@Service
public class ProductServiceImpl implements ProductService {
	@Autowired
	private ProductMapper productMapper;

	@Autowired
	private ImageMapper imageMapper;

	@Override
	public List<Product> findAll() {
		ProductExample example = new ProductExample();
		List<Product> list = this.productMapper.selectByExample(example);
		return list;
	}

	@Override
	public long count() {
		ProductExample example = new ProductExample();
		return this.productMapper.countByExample(example);
	}

	@Override
	public List<Product> findPageList(ProductSearchConditionVo vo) {
		List<Product> list = this.productMapper.selectPageList(vo);
		return list;
	}

	@Override
	public void del(Long pid) {
		this.productMapper.deleteByPrimaryKey(pid);
	}

	@Override
	public void batchDel(List<Long> pid) {
		ProductExample example = new ProductExample();
		example.createCriteria().andPidIn(pid);
		this.productMapper.deleteByExample(example);
	}

	@Override
	@Transactional
	public void save(Product product) {
		Date time = new Date(System.currentTimeMillis());
		if (product.getOnSale() == null) {
			product.setOnSale(false);
		} else {
			product.setOnSaleTime(time);
		}

		product.setCreateTime(time);
		product.setLastModifiedTime(time);
		this.productMapper.insert(product);

		if (product instanceof ProductAddVo) {
			ProductAddVo vo = (ProductAddVo) product;
			List<String> imgs = vo.getImgs();
			if (imgs != null && imgs.size() > 0) {
				for (String img : imgs) {
					Image image = new Image();
					image.setPid(product.getPid());
					image.setUrl(img);
					this.imageMapper.insert(image);
				}
			}
		}

	}

	@Override
	public Product findById(Long pid) {
		return this.productMapper.selectByPrimaryKey(pid);
	}

	@Override
	public long countByExample(ProductSearchConditionVo vo) {
		ProductExample example = new ProductExample();
		Criteria criteria = example.createCriteria();
		if (vo.getPid() != null) {
			criteria.andPidEqualTo(vo.getPid());
		}
		if (vo.getTitle() != null) {
			criteria.andTitleLike("%" + vo.getTitle() + "%");
		}
		if (vo.getOnSale() != null) {
			criteria.andOnSaleEqualTo(vo.getOnSale());
		}

		return this.productMapper.countByExample(example);
	}

	@Override
	@Transactional
	public void updata(Product product) {
		Long pid = product.getPid();
		this.productMapper.updateByPrimaryKeySelective(product);
		if (product instanceof ProductAddVo) {
			ProductAddVo vo = (ProductAddVo) product;
			List<String> imgs = vo.getImgs();
			if (imgs != null && imgs.size() > 0) {
				for (String img : imgs) {
					Image image = new Image();
					image.setPid(pid);
					image.setUrl(img);
					this.imageMapper.insertSelective(image);
				}
			}
		}
	}

}
